from sqlalchemy.orm import Session
from core.response import resp_500
from core.logger import logger
from app.monitor_doris.models.monitor_doris_http_url import MonitorDorisHttpUrl
from common.utils import CommonTimeUtils

class MonitorDorisHttpUrlHandler():
    """  MonitorDorisHttpUrl Handler
    """
    def __init__(self):
        pass


    def get_http_url_search(self, db: Session, page: int, per_page: int, field: str, value: str):
        """
        根据表字段模糊匹配，获取http的url信息
        """
        try:
            field_info = {field: value}
            order_info = {"url": "desc"}
            result = MonitorDorisHttpUrl.filter_by_field_search(db, page, per_page, field_info, order_info)
            if not result:
                logger.warning(f"get http url search, result is null => {field}:{value}")
                return False, None

        except Exception as e:
            message = f"get http url search by {field}:{value} error"
            logger.exception(f"{message} => {e}")
            return resp_500(message)

        return True, result



    def get_http_url_match(self, db: Session, page: int, per_page: int, field: str, value: str):
        """
        根据表字段完全匹配，获取http的url信息
        """
        try:
            result = MonitorDorisHttpUrl.filter_by_field_match(db, page, per_page, {field: value})
            if not result:
                logger.warning(f"get http url match, result is null => {field}:{value}")
                return False, None

        except Exception as e:
            message = f"get http url match by {field}:{value} error"
            logger.exception(f"{message} => {e}")
            return resp_500(message)

        return True, result




    def update_http_url(self, db: Session, data: dict):
        """
        修改http的url信息
        """
        try:
            http_url_object = MonitorDorisHttpUrl.get_object_by_uuid(db, data["uuid"])
            if not http_url_object:
                # 如果不存在， 则异常返回
                logger.error(f"update http url failed,{data['uuid']} not exist")
                return False

            result = MonitorDorisHttpUrl.update(db, data)
            if not result:
                logger.error(f"update http url to database failed")
                return False
        except Exception as e:
            message = f"update http url error "
            logger.exception(f"{message} => {e}")
            return resp_500(message)

        return True



    def delete_http_url(self, db: Session, data: dict):
        """
        删除http的url信息
        """
        try:
            http_url_object = MonitorDorisHttpUrl.get_object_by_uuid(db, data['uuid'])
            if not http_url_object:
                # 如果不存在， 则异常返回
                logger.error(f"delete http url failed, uuid:{data['uuid']} not exist")
                return False

            # 删除告警记录
            MonitorDorisHttpUrl.delete_object_by_uuid(db, data['uuid'])
        except Exception as e:
            message = f"delete http url uuid:{data['uuid']}  error"
            logger.exception(f"{message} => {e}")
            return resp_500(message)

        return True


